package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.exception.FoodException;
import com.woniuxy.commons.exception.code.FoodExceptionCode;
import com.woniuxy.commons.model.Result;
import com.woniuxy.commons.utils.PagesUtils;
import com.woniuxy.controller.form.FoodVarietyForm;
import com.woniuxy.controller.form.GetAllMyFoodForm;
import com.woniuxy.controller.form.PageInfoForm;
import com.woniuxy.dao.FoodVarietyDao;
import com.woniuxy.dao.model.FoodVariety;
import com.woniuxy.service.FoodVarietyService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Api(tags = "菜品信息操作接口,菜品的增删改查")
@RestController
@RequestMapping("/foodVariety")
public class FoodVarietyController {
    @Autowired
    private FoodVarietyService foodVarietyService;
    @Resource
    private FoodVarietyDao foodVarietyDao;

    /**
     * @param storeId    商家id
     * @param productsId 商品类别id
     * @return 菜品分类展示
     */
    @ApiOperation("菜品分类信息查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商家id"),
            @ApiImplicitParam(name = "productsId", value = "餐品类别id")
    })
    @GetMapping("list/{storeId}/{productsId}")
    public Result showFoodByStoreIdAndProductsId(@PathVariable String storeId,
                                                 @PathVariable String productsId,
                                                 PageInfoForm pageInfoForm) {

        if (storeId.equals("undefined"))
            return Result.fail(500, "商家id不存在");
        if (productsId.equals("undefined"))
            return Result.fail(500, "餐品类别id不存在");
        return Result.success(200,
                "菜品分类信息展示成功",
                PagesUtils.getPages(pageInfoForm.getPageNum() == null ? 1 : pageInfoForm.getPageNum(),
                        pageInfoForm.getPageSize() == null ? 2 : pageInfoForm.getPageSize(),
                        foodVarietyService.getFoodByStoreIdAndProductsId(Long.parseLong(storeId),
                                Long.parseLong(productsId))));
    }

    /**
     * @param storeId    商家id
     * @param productsId 商品类别id
     * @return 菜品分类展示
     */
    @ApiOperation("菜品分类信息查询(未分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商家id"),
            @ApiImplicitParam(name = "productsId", value = "餐品类别id")
    })
    @GetMapping("list2/{storeId}/{productsId}")
    public Result showFoodByStoreIdAndProductsId2(@PathVariable String storeId,
                                                  @PathVariable String productsId) {

        if (storeId.equals("undefined"))
            return Result.fail(500, "商家id不存在");
        if (productsId.equals("undefined"))
            return Result.fail(500, "餐品类别id不存在");
        return Result.success(200,
                "菜品分类信息展示成功",
                foodVarietyService.getFoodByStoreIdAndProductsIdLimit8(Long.parseLong(storeId),
                        Long.parseLong(productsId)));
    }

    /**
     * @param storeId 商家id
     * @return 菜品分类展示
     */
    @ApiOperation("查询商家所有菜品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商家id"),
    })
    @GetMapping("list/{storeId}")
    public Result showFoodByStoreId(@PathVariable String storeId) {

        if (storeId.equals("undefined"))
            return Result.fail(500, "商家id不存在");
        QueryWrapper<FoodVariety> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("storeId", storeId);
        return Result.success(200,
                "菜品分类信息展示成功",
                foodVarietyDao.getFoodByStoreId2(Long.parseLong(storeId)));
    }

    /**
     * @param productsId 餐品id
     * @return 菜品分类展示
     */
    @ApiOperation("通过餐品id查询菜品信息(5条)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productsId", value = "餐品id"),
    })
    @GetMapping("listP/{productsId}")
    public Result showFoodByPro(@PathVariable String productsId) {

        if (productsId.equals("undefined"))
            return Result.fail(500, "商家id不存在");
        return Result.success(200,
                "菜品分类信息展示成功",
                foodVarietyDao.getFoodByPro(Long.parseLong(productsId)));
    }

    /**
     * @param productsId 餐品id
     * @return 菜品分类展示
     */
    @ApiOperation("通过餐品id查询菜品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productsId", value = "餐品id"),
    })
    @GetMapping("listP2/{productsId}")
    public Result showFoodByPro2(@PathVariable String productsId) {

        if (productsId.equals("undefined"))
            return Result.fail(500, "商家id不存在");
        return Result.success(200,
                "菜品分类信息展示成功",
                foodVarietyDao.getFoodByPro2(Long.parseLong(productsId)));
    }

    /**
     * @param id 菜品id
     * @return 菜品删除信息
     */
    @ApiOperation("根据菜品id删除")
    @ApiImplicitParam(name = "id", value = "菜品id")
    @DeleteMapping("del/{id}")
    public Result delFoodById(@PathVariable Integer id) {
        if (id != null)
            if (foodVarietyService.removeById(id))
                return Result.success(200, "菜品删除成功", null);
        return Result.fail(500, "菜品删除失败");
    }

    /**
     * @param foodVarietyForm 菜品参数信息
     * @return 菜品添加信息(boolean)
     */
    @ApiOperation("菜品新增")
    @PostMapping("add")
    public Result addFood(@RequestBody FoodVarietyForm foodVarietyForm) {
        return Result.success(200, "菜品添加成功",
                foodVarietyService.addFood(foodVarietyForm));
    }

    /**
     * @param id              菜品id
     * @param foodVarietyForm 菜品修改信息参数
     * @return 修改后的菜品信息
     */
    @ApiOperation("通过菜品id,修改菜品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜品id")
    })
    @PutMapping("update/{id}")
    public Result updateFood(@PathVariable String id, @RequestBody FoodVarietyForm foodVarietyForm) {
        if (id.equals("") || id.equals("undefined"))
            return Result.fail(200, "id不存在");
        return Result.success(200, "菜品修改成功",
                foodVarietyService.updateFood(Long.parseLong(id), foodVarietyForm));
    }
    /**
     * 通过关键字搜索菜品信息
     * @param
     * @return
     */
    @ApiOperation("通过关键字搜索菜品信息")
    @ApiImplicitParam(name = "keyword", value = "菜品搜索关键字")
    @GetMapping("search/{keyword}")
    public Result getFoodByKeyword(@PathVariable String keyword) {
        return Result.success(200, "菜品搜索成功",
                foodVarietyService.getFoodByKeyword(keyword));
    }

    /**
     * 后台查询自己的分类所有菜品，包含条件查询
     * @param
     * @return
     */
    @ApiOperation("后台查询自己的分类所有菜品，包含条件查询")
    @GetMapping("/getMyFood/{productsId}/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productsId",value = "餐品id"),
            @ApiImplicitParam(name = "pageNum",value = "当前页"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量"),
    })
    public Result getMyFood(@PathVariable("productsId") Long productsId, @PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {


        Page<FoodVariety> myFood = foodVarietyService.getMyFood(productsId, pageNum, pageSize);

        return Result.success(200, "查询我的菜品成功", myFood);
    }

    /**
     * 后台查询自己的所有菜品，包含条件查询
     * @param
     * @return
     */
    @ApiOperation("后台查询自己的所有菜品，包含条件查询")
    @PostMapping("/getAllMyFood")
    public Result getAllMyFood(@RequestBody GetAllMyFoodForm gmf) {
        Page<FoodVariety> allMyFood = foodVarietyService.getAllMyFood(gmf.getPageNum(), gmf.getPageSize());

        return Result.success(200, "查询我的菜品成功", allMyFood);
    }

    /**
     * 通过菜品Id查询菜品的信息的接口
     * @param id 菜品Id
     * @return
     */
    @ApiOperation("通过id查询菜品")
    @ApiImplicitParam(name = "id", value = "菜品id")
    @GetMapping("/getFoodById/{id}")
    public Result getFoodById(@PathVariable("id") Long id) {

        FoodVariety foodVariety = foodVarietyService.getById(id);

        return Result.success(200, "根据菜品id查询菜品信息成功", foodVariety);
    }

    /**
     * 商家上架菜品
     * @param id 菜品id
     * @return
     */
    @GetMapping("/upFood")
    @ApiOperation("商家上架菜品")
    @ApiImplicitParam(name = "id", value = "菜品Id")
    public Result upFood(Long id){

        FoodVariety foodVariety = foodVarietyService.getById(id);
        if(foodVariety.getState() == 1){
            throw new FoodException(FoodExceptionCode.FOOD_HAS_UP);
        }
        foodVariety.setState(1);

        foodVarietyService.update(foodVariety,new QueryWrapper<FoodVariety>().eq("id",id));

        return Result.success(200,"上架成功",null);
    }

    /**
     * 商家下架菜品
     * @param id 菜品id
     * @return
     */
    @GetMapping("/offFood")
    @ApiOperation("商家下架菜品")
    @ApiImplicitParam(name = "id", value = "菜品Id")
    public Result offFood(Long id){

        FoodVariety foodVariety = foodVarietyService.getById(id);
        if(foodVariety.getState() == 0){
            throw new FoodException(FoodExceptionCode.FOOD_HAS_OFF);
        }
        foodVariety.setState(0);

        foodVarietyService.update(foodVariety,new QueryWrapper<FoodVariety>().eq("id",id));

        return Result.success(200,"下架成功",null);
    }

//    @ApiOperation("查询所有菜品信息")
//    @GetMapping("/getFoodList")
//    public Result getFoodList() {
//        return Result.success(200, "查询所有菜品信息成功",
//                foodVarietyService.list());
//    }


}

